package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;

	@Operation(summary = "更新VIP到期失效状态")
	@GetMapping("/updateVipExpireStatus")
	public Result updateVipExpireStatus(){
		userInfoService.updateVipExpireStatus();

		return Result.ok();

	}


	@Operation(summary = "处理用户购买记录")
	@PostMapping("/savePaidRecord")
	public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo){
		int result = userInfoService.savePaidRecord(userPaidRecordVo);

		if (result>0){

			return Result.ok();
		}
		return Result.fail();

	}


	@TsLogin
	@Operation(summary = "获取专辑已支付的声音Id集合列表")
	@GetMapping("/findUserPaidTrackList/{albumId}")
	public Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId){

		Long userId = AuthContextHolder.getUserId();
		List<Long> list = this.userInfoService.findUserPaidTrackList(albumId,userId);

		return Result.ok(list);
	}


	@Operation(summary = "判断用户是否购买过专辑")
	@GetMapping("/isPaidAlbum/{albumId}")
	public Result<Boolean> isPaidAlbum(@PathVariable("albumId") Long albumId){
		Long userId = AuthContextHolder.getUserId();
		Boolean flag = userInfoService.isPaidAlbum(albumId,userId);
		return Result.ok(flag);
	}



	@TsLogin
	@Operation(summary ="判断用户是否购买过声音列表")
	@PostMapping("/userIsPaidTrack/{albumId}")
	public Result<Map<Long, Integer>> userIsPaidTrack(@RequestBody List<Long> trackNeedPaidList, @PathVariable("albumId")  Long albumId){
		Long userId = AuthContextHolder.getUserId();
		Map<Long,Integer> map =  userInfoService.userIsPaidTrack(albumId,trackNeedPaidList,userId);

		return Result.ok(map);
	}




	@Operation(summary = "根据用户id获取用户信息")
	@GetMapping("/{userId}")
	public Result<UserInfoVo> getUserInfo(@PathVariable Long userId){
		UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);

		return Result.ok(userInfoVo);

	}

}

